package com.colorful.insuranceproject.apush;

import android.content.Context;

import com.alibaba.sdk.android.push.notification.CPushMessage;
import com.colorful.insuranceproject.bean.PushMessage;
import com.lovely3x.common.utils.ALog;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by handsome-Bob on 2017/11/11.
 * <p>
 * APush的操作类
 */
public class APushProcessor implements MyMessageReceiver.APushListener {

    private List<MsgPassThrough> list_callback = new ArrayList<>();

    private static final String TAG = "APushProcessor";

    private static Context mContext;

    public static final APushProcessor instance = new APushProcessor();

    /**
     * 将自己注册到消息推送管理器中
     */
    public static void registerSelfToPushManager(Context context) {
        mContext = context;
        MyMessageReceiver.getInstance().registerMessageListener(instance, false);
    }

    private APushProcessor() {

    }

    public static APushProcessor getInstance() {
        return instance;
    }

    @Override
    public int getPriority() {
        return 0;
    }

    /**
     * 通知
     */
    @Override
    public boolean onNotificationPush(Context context, String title, String summary, Map<String, String> extraMap) {
        ALog.e(TAG, "Receive notification, title: " + title + ", summary: " + summary + ", extraMap: " + extraMap);
        return false;
    }

    /**
     * 消息
     */
    @Override
    public boolean onMessagePush(Context context, CPushMessage cPushMessage) {
        ALog.e(TAG, "onMessage, messageId: " + cPushMessage.getMessageId() + ", title: " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());
        PushMessage pushMessage = new PushMessage();
        pushMessage.setAppId(cPushMessage.getAppId());
        pushMessage.setTitle(cPushMessage.getTitle());
        pushMessage.setContent(cPushMessage.getContent());
        pushMessage.setTraceInfo(cPushMessage.getTraceInfo());
        TransportPushMessage(pushMessage);//  传递标题栏的值
        return false;
    }

    /**
     * 被打开后的通知做操作
     */
    @Override
    public boolean onNotificationOpenedPush(Context context, String title, String summary, String extraMap) {
        ALog.e(TAG, "onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
        return false;
    }

    /**
     * 被点击后的通知不做操作
     */
    @Override
    public boolean onNotificationClickedWithNoActionPush(Context context, String title, String summary, String extraMap) {
        ALog.e(TAG, "onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
        return false;
    }

    /**
     * 透传出来的属性
     *
     * @param pushMessage 传递出来的对象
     */
    private void TransportPushMessage(PushMessage pushMessage) {
        Iterator<MsgPassThrough> callback = list_callback.iterator();
        while (callback.hasNext()) {
            MsgPassThrough next = callback.next();
            if (next.onPayStatus(pushMessage)) {
                return;
            }
        }
    }

    /**
     * 消息回调
     */
    public interface MsgCallBack {
        boolean onCommonMsg(PushMessage pushMessage);
    }

    /**
     * 消息透传
     */
    public interface MsgPassThrough extends MsgCallBack {
        boolean onPayStatus(PushMessage pushMessage);
    }

    public boolean registerCallBack(MsgPassThrough callBackMessage) {
        return this.list_callback.add(callBackMessage);
    }

    public boolean unregisterCallBack(MsgPassThrough callBackMessage) {
        return this.list_callback.remove(callBackMessage);
    }
}
